
/*
 * Copyright 2010, Center for Advanced Information Systems,Nanyang Technological University
 *
 * File name: FGraph.java
 *
 * Abstract:     The structure of MF-index
 *
 * Current Version:      0.1
 * Auther:               Jin Changjiu
 * Modified Date:        June.22,2010
 *
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.frequentindex;

import gblend.adjlistgraph.ALGraph;
import gblend.adjlistgraph.Link;
import gblend.adjlistgraph.Node;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Stack;
import java.util.Vector;

/**
 *
 * @author cjjin
 */
public class FGraph {

    private Link[] adjTab = null;
    private ALGraph[] vertexs;
    private int pos = -1;
    //   private static Vector<Integer> rootlist = new Vector<Integer>();
    private Hashtable<Integer, String> clusterHeadSet = new Hashtable<Integer, String>();
    private HashMap<String, Integer> cammapidx = new HashMap<String, Integer>();

    public FGraph(int size) {
        System.out.println("Construct FGraph of size " + size);
        vertexs = new ALGraph[size];
        pos = -1;
        adjTab = new Link[size];
        for (int i = 0; i < size; i++) {
            adjTab[i] = new Link();
        }
    }

    /*public void setroot(int graphid)
    {
    rootlist.addElement(graphid);
    }

    public Vector<Integer> getroot()
    {
    return rootlist;
    }*/
    public void addnode(ALGraph value) {
        vertexs[++pos] = value;
        cammapidx.put(value.getCam(), pos);
    }

    public int getIndex(String cam) {
        Integer idx = cammapidx.get(cam);
        if (idx == null) {
            return -1;
        } else {
            return idx;
        }
    }

    public int getSize() {
        return pos + 1;
    }

    public ALGraph getNode(int index) {
        return vertexs[index];
    }

    public int getEdge(int from, int to) {

        Node p = adjTab[from].first();
        while (p != null) {
            if (p.index() == to) {
                return 1;
            }
            p = p.next();
        }

        return 0;
    }

    public void addEdge(int from, int to) {
        adjTab[from].add(to);
    }

    public int findNeighbor(int index) {
        for (int i = 0; i <= pos; i++) {
            if (getEdge(index, i) == 1 && !vertexs[i].isVisited()) {
                return i;
            }
        }
        return -1;
    }

    public void dsf(int index) {
        int inital = index;
        if (vertexs[index] == null) {
            return;
        }

        Stack s = new Stack();
        vertexs[index].visit();
        s.push(index);

        while (!s.isEmpty()) {

            index = findNeighbor((Integer) s.peek());
            if (index != -1) {
                vertexs[index].visit();
                s.push(index);
                vertexs[inital].setSucc(index);
            } else {
                s.pop();
            }
        }

        clean();
    }

    public void clean() {
        for (ALGraph v : vertexs) {
            if (v != null) {
                v.clean();
            }
        }
    }

    public Hashtable<Integer, String> getClusterHeads() {
        return clusterHeadSet;
    }

    public void setClusterHeads(int id, String cam) {
        clusterHeadSet.put(id, cam);
    }
}
